//一次性收集表单数据并以json方式返回
$.fn.form2Json = function() {
	var formId = "#" + $(this).attr("id");
	var json = "";
	var radios = "";
	var checks = "";
	var texts = "";
	var combos = "";
	var textareas = "";
	var selector = formId + " input";//找到表单中所有的input标签
	var comboSelector = formId + " select"; //找到表单中所有select combo对象
	var textareaSelector = formId + " textarea"; //找到表单的大文本输入框
	// 处理大文本框
	for (i = 0; i < $(textareaSelector).length; i++) {
		var textareaelement = textareaSelector + ":eq(" + i + ")";
		var textareaname = $(textareaelement).attr("name");
		if (textareas.indexOf(textareaname) == -1) {
			textareas = (textareas.length == 0 ? "" : textareas + ",")
					+ textareaname + ":\""
					+ $("textarea[name='" + textareaname + "']").val() + "\"";
		}
	}
	// 处理下拉列表框
	for (i = 0; i < $(comboSelector).length; i++) {
		var comboelement = comboSelector + ":eq(" + i + ")";
		var comboname = $(comboelement).attr("name");
		if (combos.indexOf(comboname) == -1) {
			combos = (combos.length == 0 ? "" : combos + ",") + comboname
					+ ":\"" + $("select[name='" + comboname + "']").val() + "\"";
		}
	}
	// 处理input对象
	for (i = 0; i < $(selector).length; i++) {
		var element = selector + ":eq(" + i + ")";
		var etype = $(element).attr("type");
		// 处理文本框
		if (etype == "text" || etype == "password"|| etype == "hidden") {
			var tkname = $(element).attr("name");
			if (texts.indexOf(tkname) == -1) {
				texts = (texts.length == 0 ? "" : texts + ",") + tkname + ":\""+ $("input[name='" + tkname + "']").val() + "\"";
			}
		}
		// 处理单选框
		if (etype == "radio") {
			var rkname = $(element).attr("name");
			if (radios.indexOf(rkname) == -1) {
				var checkedcount = $("input[name='" + rkname + "']:checked").length;
				radios = (radios.length == 0 ? "" : radios + ",")+ rkname+ ":\""+ (checkedcount == 0 ? "" : $(
								"input[name='" + rkname + "']:checked").val())+ "\"";
			}
		}
		// 处理多选框
		if (etype == "checkbox") {
			var ckname = $(element).attr("name");
			// alert(ckname);
			// 1\检查checks是否已经此checkbox
			if (checks.indexOf(ckname + ":") == -1) { // 以前没有找到过
				//2\没有，则将其值存入checks
				var tempchecks = "";
				if ($("input[name='" + ckname + "']:checked").length > 0) {
					for (j = 0; j < $("input[name='" + ckname + "']:checked").length; j++) {
						tempchecks = (tempchecks.length == 0 ? "" : tempchecks+"|")
								+ $("input[name='"+ckname+"']:checked:eq("+j+")").val();
					}
				}
				checks = (checks.length == 0 ? "" : checks + ",") + ckname+ ":\"" + tempchecks + "\"";
			}
		}
	}
	json = (json.length == 0 ? "" : json + ",")
			+ (texts.length == 0 ? "" : texts + ",")
			+ (combos.length == 0 ? "" : combos + ",")
			+ (radios.length == 0 ? "" : radios + ",")
			+ (textareas.length == 0 ? "" : textareas + ",")
			+ (checks.length == 0 ? "" : checks + ",")+"";
	if(json.indexOf(",", 0)){
		json = json.substring(0, json.length);
	}
	if(json.indexOf(",", json.length-1)){
		json = json.substring(0, json.length-1);
	}
	json="{"+json+"}"
	var jsonData = eval('('+json+')');
	return json;
}

//表单数据
$.fn.collectionFormData = function() {
	var jsonStr = '{';
	//查找所有的input
	var inputForm = $(this).find(":input");
	for(i = 0; i < inputForm.length; i++){
		//得到类型
		var inputType = inputForm[i].getAttribute("type");
		//得到名字
		var inputName = inputForm[i].getAttribute("name");
		//得到值
		var inputValue = inputForm[i].value;
		// 处理文本框
		if (inputType == "text" || inputType == "password"|| inputType == "hidden") {
			if(i==inputForm.length){
				if(inputValue==""||inputValue==null){
				   jsonStr+="\""+inputName+"\":\"\"";
				}else{
				   jsonStr+="\""+inputName+"\":\""+inputValue+"\"";
				}
			}else{
			   if(inputValue==""||inputValue==null){
				   jsonStr+="\""+inputName+"\":\"\",";
				}else{
				   jsonStr+="\""+inputName+"\":\""+inputValue+"\",";
				}
			}
		}
	}
	if(jsonStr.indexOf(",", 0)){
		jsonStr = jsonStr.substring(0, jsonStr.length-1);
	}
	jsonStr+='}';
	var json = (new Function("return " + jsonStr))();
	return json;
}


